package main

import (
	"fmt"
	"math"
	"sort"
)

func Round(f float64, n int) float64 {
	p10n := math.Pow10(n)
	return math.Trunc((f+0.5/p10n)*p10n) / p10n
}

type Triangle struct {
	a float64
	b float64
	c float64
}

func NewTriangle(a, b, c float64) *Triangle {
	return &Triangle{a: a, b: b, c: c}
}

func (t *Triangle) Area() float64 {
	s := (t.a + t.b + t.c) / 2
	return Round(math.Sqrt(s*(s-t.a)*(s-t.b)*(s-t.c)), 2)
}

type Cicle struct {
	r float64
}

func NewCicle(radius float64) *Cicle {
	return &Cicle{r: radius}
}

func (c *Cicle) Area() float64 {
	return Round(c.r*c.r*math.Pi, 2)
}

type Rectangle struct {
	length float64
	width  float64
}

func NewRectangle(length, width float64) *Rectangle {
	return &Rectangle{length: length, width: width}
}
func (s *Rectangle) Area() float64 {
	return Round(s.length*s.width, 2)
}

type Areaer interface {
	Area() float64
}

func Area(a Areaer) float64 {
	return a.Area()
}

func main() {
	t1 := NewTriangle(3, 4, 5)
	c1 := NewCicle(5)
	s1 := NewRectangle(5, 6)

	shapers := []Areaer{t1, c1, s1}
	fmt.Println("------排序前------")
	for _, s := range shapers {
		fmt.Println(s.Area())
	}

	//排序
	sort.SliceStable(shapers, func(i, j int) bool {
		return shapers[i].Area() > shapers[j].Area()
	})

	fmt.Println("------排序后------")
	for _, s := range shapers {
		fmt.Println(s.Area())
	}
}
